<?php
namespace app\api\controller;
use app\api\validate\OrderValidate;
use app\common\controller\Api;
use app\admin\model\Order as OrderModel;
use think\Db;

class Order extends Api{
    protected $noNeedRight = '*';
    protected $noNeedLogin = ["payinfo"];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**支付信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: zhaoyu
     * @Time: 2024/1/25 17:39
     */
    public function payinfo(){
        $payinfo = Db::table("zwa_shopro_config")
            ->where("code", "zwa.pay")
            ->find();
        $this->success("", json_decode($payinfo["value"]));
    }


    /**商品详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: zhaoyu
     * @Time: 2024/1/25 17:39
     */
    public function detail(){
        $id = $this->request->get("id");
        if($id){
            $order = OrderModel::where(['id'=>$id, 'user_id'=>$this->auth->getUser()->id])->with(['goods'])->find();
            $this->success("", $order);
        }
        $this->error("订单id不存在");
    }


    /**提交订单
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: zhaoyu
     * @Time: 2024/1/25 16:31
     */
    public function create()
    {
        (new OrderValidate())->scene('create')->goCheck();
        $data = $this->request->post();

        $uid = $this->auth->getUser()->id;
        $company= Db::name('company')->where(['user_id'=>$uid])->find();
        if(empty($company)){
            $this->error('请填写企业信息');
        }
        $goods = Db::name('goods')->where(['id' => $data['goods_id']])->find();
        if (!$goods) {
            $this->error("商品不存在");
        }
        $time = date('Y-m-d H:i:s', time());
        $insertData = [
            'goods_id' => $data['goods_id'],
            'user_id' => $uid,
            'company_id' => $company['id'],
            'order_no' => makeOrderNo(),
            'name' => $goods['title'],
            'content' => $data['content'],
            'status' => 0,
            'create_time' => $time,
            'update_time' => $time,
            'price' => $goods['price'],
            'lat' => $data['lat'],
            'lng' => $data['lng'],
            'address' => $data['address'],
            'link_name' => $data['link_name'],
            'link_phone' => $data['link_phone'],
        ];
        $order_id = Db::name('order')->insertGetId($insertData);
        if(!$order_id){
            $this->error('添加订单失败');
        }
        $returnData=$insertData;
        $returnData['id'] =$order_id;
        $returnData['voucher_img'] ='';
        $this->success("提交订单成功",$returnData);
    }


    public function list(){
        $status = $this->request->get("status");
        $model = new OrderModel();
        $where = [
            "user_id"=>$this->auth->getUser()->id
        ];
        if(isset($status)){
            $where["status"] = $status;
        }
        $list = $model->where($where)
            ->with(['goods'])
            ->order("id", "desc")
          ->paginate($this->request->param('list_rows', 10));
//        $list->each(function ($item){
//            $item['createtime']=  $item['createtime'] ? date('Y-m-d H:i:s', $item['createtime']):'';
//            return $item;
//        });
        $this->success($this->auth->getUser()->id, $list);
    }

    //更新订单信息
    public function update(){
        if($this->request->isPost() === false){
            $this->error('支付方式错误');
        }
        $post= $this->request->param();
        if(!isset($post['voucher_img']) || $post['voucher_img'] ==''){
            $this->error('请上传支付凭证');
        }
        $uid=$this->auth->getUser()->id;
        $orderModel= new OrderModel();
        $info = $orderModel->where(['id'=>$post['id'],'user_id'=>$uid])
            ->find();

        if(empty($info)){
            $this->error('订单不存在');
        }
        if($info['status']  > 0){
            $this->error('订单状态不对');
        }
        //todo鉴权
        $insertData=[
            'voucher_img'=>$post['voucher_img'],
            'status'=>1,
            'update_time'=>date('Y-m-d',time())
        ];
        $row=$orderModel->where(['id'=>$post['id']])->update($insertData);
        if($row){
            $this->success('请耐心等待，后期工作人员会联系您');
        }else{
            $this->error('订单不存在');
        }
//        OrderModel::update($insertData, ["id"=>$data["id"]]);

    }
}
